<?php
namespace App\Library;

require_once('DES/DES.php');

error_reporting(0);
header("Content-Type: text/html; charset=utf-8");

class Crypt {
    protected static $dwkey = '533018dd5b91123d1231231';
    protected static $key = '33725169';

    /**
     * 车俩违法信息
     * @param $hphm
     * @param $hpzl
     * @param $clsbdh
     * @return array|bool|string
     */
    public function get_car_wf($hphm, $hpzl, $clsbdh) {
        if(empty($hphm)) {
            return ['status' => false, 'data' => '车辆号牌号码不能为空'];
        }
        if(empty($hpzl)) {
            return ['status' => false, 'data' => '号牌种类不能为空'];
        }
        if(empty($clsbdh)) {
            return ['status' => false, 'data' => '车辆识别号后六位不能为空'];
        }
        $des = new \Crypt_DES();
        $des->setKey(self::$key);  //加密密码
        $data['dwkey']  = self::$dwkey;
        $data['hphm']   = $hphm;
        $data['hpzl']   = $hpzl;
        $data['clsbdh'] = $clsbdh;
        $data_js        = json_encode($data);
        $mm             = base64_encode($des->encrypt($data_js));
        $url1           = "http://sanfangcx.xmxing.net/car_wf.php";
        $nn             = $this->post($url1, ["data" => $mm]);
        $m              = $des->decrypt(base64_decode($nn));
        if($m == "") {
            return false;
        } else {
            $data=json_decode($m,true);
            if($data['state']=='0'){
                return ['status'=>'true','data'=>$data['data']];
            }
        }
        return false;
    }

    /**
     * 驾驶人违法信息查询
     * @param $sfzhm
     * @param $dabh
     * @return array|bool|string
     */
    public function get_jsr_wf($sfzhm, $dabh) {
        if(empty($sfzhm)) {
            return ['status' => false, 'data' => '驾驶证号不能为空'];
        }
        if(empty($dabh)) {
            return ['status' => false, 'data' => '档案编号不能为空'];
        }
        $des = new \Crypt_DES();
        $des->setKey(self::$key);  //加密密码
        $data['dwkey'] = self::$dwkey;
        $data['sfzhm'] = $sfzhm;
        $data['dabh']  = $dabh;
        $data_js       = json_encode($data);
        $mm            = base64_encode($des->encrypt($data_js));
        $url1          = "http://sanfangcx.xmxing.net/jsr_wf.php";
        $nn            = $this->post($url1, ["data" => $mm]);
        $m             = $des->decrypt(base64_decode($nn));
        if($m == "") {
            return false;
        } else {
            $data=json_decode($m,true);
            if($data['state']=='0'){
                return ['status'=>'true','data'=>$data['data']];
            }
        }
        return false;
    }

    /**
     * 驾驶人信息查询
     * @param $sfzhm
     * @param $dabh
     * @return array|bool|string
     */
    public function get_jsr_info($sfzhm, $dabh) {
        if(empty($sfzhm)) {
            return ['status' => false, 'data' => '驾驶证号不能为空'];
        }
        if(empty($dabh)) {
            return ['status' => false, 'data' => '档案编号不能为空'];
        }

        $des = new \Crypt_DES();
        $des->setKey(self::$key);  //加密密码
        $data['dwkey'] = self::$dwkey;
        $data['sfzhm'] = $sfzhm;
        $data['dabh']  = $dabh;
        $data_js       = json_encode($data);
        $mm            = base64_encode($des->encrypt($data_js));
        $url1          = "http://sanfangcx.xmxing.net/jsr_info.php";
        $nn            = $this->post($url1, ["data" => $mm]);
        $m             = $des->decrypt(base64_decode($nn));

        if($m == "") {
            return false;
        } else {
            $data=json_decode($m,true);
            if($data['state']=='0'){
                return ['status'=>'true','data'=>$data['data']];
            }
        }
        return false;
    }

    public function post($url, $post = null) {
        if(is_array($post)) {
            ksort($post);
            $content        = http_build_query($post);
            $content_length = strlen($content);
            $options        = [
                'http' => [
                    'method'  => 'POST',
                    'header'  => "Content-type: application/x-www-form-urlencoded\r\n" . "Content-length: $content_length\r\n",
                    'content' => $content
                ]
            ];
            return file_get_contents($url, false, stream_context_create($options));
        }
    }
}


